package com.zb.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>添加描述信息</p>
 *
 * @author 李柯
 * @since 2022年11月15日 10:46
 */
@Configuration
public class MQConfig {

    public static final String PAY_EXCHANGE = "pay_exchange";
    public static final String PAY_QUEUE = "pay_queue";
    public static final String PAY_ROUTING = "pay_routing";

    @Bean(PAY_EXCHANGE)
    public Exchange createPayExchange() {
        return ExchangeBuilder.topicExchange(PAY_EXCHANGE).durable(true).build();
    }

    @Bean(PAY_QUEUE)
    public Queue createPayQueue() {
        return new Queue(PAY_QUEUE, true, false, false, null);
    }

    @Bean
    public Binding bindingPayExchangeAndQueue(@Qualifier(PAY_EXCHANGE) Exchange exchange, @Qualifier(PAY_QUEUE) Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(PAY_ROUTING).noargs();
    }
    /******************************************************************/


    public static final String ONE_EXCHANGE = "one_exchange";
    public static final String ONE_QUEUE = "one_queue";
    public static final String ONE_ROUTING = "one_routing";

    public static final String TWO_EXCHANGE = "two_exchange";
    public static final String TWO_QUEUE = "two_queue";
    public static final String TWO_ROUTING = "two_routing";

    @Bean(ONE_EXCHANGE)
    public Exchange oneExchange() {
        return ExchangeBuilder.topicExchange(ONE_EXCHANGE).durable(true).build();
    }

    @Bean(ONE_QUEUE)
    public Queue oneQueue() {
        Map<String, Object> params = new HashMap<>();
        params.put("x-dead-letter-exchange", TWO_EXCHANGE);
        params.put("x-dead-letter-routing-key", TWO_ROUTING);
        return new Queue(ONE_QUEUE, true, false, false, params);
    }

    @Bean
    public Binding oneBinding(@Qualifier(ONE_EXCHANGE) Exchange exchange, @Qualifier(ONE_QUEUE) Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(ONE_ROUTING).noargs();
    }

    @Bean(TWO_EXCHANGE)
    public Exchange twoExchange() {
        return ExchangeBuilder.topicExchange(TWO_EXCHANGE).durable(true).build();
    }

    @Bean(TWO_QUEUE)
    public Queue twoQueue() {
        return new Queue(TWO_QUEUE, true, false, false, null);
    }

    @Bean
    public Binding twoBinding(@Qualifier(TWO_EXCHANGE) Exchange exchange, @Qualifier(TWO_QUEUE) Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(TWO_ROUTING).noargs();
    }
    /******************************************************************/

    public static final String ADD_COURSE_EXCHANGE = "addCourseExchange";
    public static final String ADD_COURSE_QUEUE = "addCourseQueue";
    public static final String ADD_COURSE_ROUTING = "addCourseRouting";

    @Bean(ADD_COURSE_EXCHANGE)
    public Exchange createaddCourseExchange() {
        return ExchangeBuilder.topicExchange(ADD_COURSE_EXCHANGE).durable(true).build();
    }

    @Bean(ADD_COURSE_QUEUE)
    public Queue createaddCourseQueue() {
        return new Queue(ADD_COURSE_QUEUE,
                true, false, false, null);
    }

    @Bean
    public Binding bindingaddCourseExchangeAndQueue(
            @Qualifier(ADD_COURSE_EXCHANGE) Exchange exchange,
            @Qualifier(ADD_COURSE_QUEUE) Queue queue) {
        return BindingBuilder.bind(queue).to(exchange).with(ADD_COURSE_ROUTING).noargs();
    }
}
